package gamesys.games.particlesystem.core {
	import gamesys.games.particlesystem.AbstractTest;

	import particlesystem.core.ParticleCanvas;
	import particlesystem.core.ParticleEffect;
	import particlesystem.core.ParticleSettings;
	import particlesystem.effects.DecayEffect;
	import particlesystem.effects.GravityEffect;
	import particlesystem.effects.LifeEffect;
	import particlesystem.effects.VelocityEffect;
	import particlesystem.graphics.BitmapAnimationFactory;
	import particlesystem.utils.Range;

	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	/**
	 * @author robertthurston
	 */
	[SWF (width="577", height="469", frameRate="30", backgroundColor="0x222222")]

	public class ParticleEffectTest1 extends AbstractTest {

		private var particleEffect : ParticleEffect;
		private var particleCanvas : ParticleCanvas;
		private var particleSettings : ParticleSettings;
		private var emitter : Sprite;
		private var emitterRotation : Number;
		private var bitmapAnimationFactory : BitmapAnimationFactory;

		override protected function init() : void {
			emitterRotation = 0;
			emitter = new Sprite();
			emitter.graphics.beginFill(0xFF0000);
			emitter.graphics.drawCircle(5, 5, 5);
			emitter.x = 275;
			emitter.y = 250;
			addChild(emitter);
			
			bitmapAnimationFactory = new BitmapAnimationFactory();
			
			particleCanvas = new ParticleCanvas(577, 469);
			
			particleSettings = new ParticleSettings();
			particleSettings.startBounds = new Rectangle(1, 1, 2, 2);
			particleSettings.startDelayRange = new Range(1, 15);
			particleSettings.reSpawn = false;
			particleSettings.emitter = emitter;
			particleSettings.bitmapAnimationParticles = [bitmapAnimationFactory.getBitmapAnimation("Coin1")];
			
			particleEffect = new ParticleEffect(particleSettings, particleCanvas);
			particleEffect.addEffect(new LifeEffect(10, 15, 10));
			particleEffect.addEffect(new VelocityEffect(1, 10, 0, 360));
			particleEffect.addEffect(new GravityEffect(1, 1));
			particleEffect.addEffect(new DecayEffect(.75, .80));
			
			addChild(particleCanvas);
			
			stage.addEventListener(MouseEvent.CLICK, onClick);
		}

		private function onClick(event : MouseEvent) : void {
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}

		private function onEnterFrame(event : Event) : void {
			particleEffect.addParticles(100);
			emitterRotation += .2;
			
			emitter.x = 275 + (100 * Math.sin(emitterRotation));
			emitter.y = 200 + (100 * -Math.cos(emitterRotation));
			
			particleCanvas.clear();
			particleEffect.update();
		}
	}
}